package ui.control
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import ui.GuiWnd;

	public class GuiWindow extends GuiWnd
	{
		private var bDrag:Boolean = true;
		private var bDown:Boolean = false;
		
		public function GuiWindow()
		{
			this.addEventListener(MouseEvent.CLICK, onClick);
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAdd);
		}

		protected function onAdd(event:Event):void
		{
			UpDataEvent();
		}
		protected function onClick(event:MouseEvent):void
		{
			this.parent && this.parent.addChild(this);
		}
		
		
		protected function onDown(event:MouseEvent):void
		{
			bDown = true;
		}
		
		
		public function get isDrag():Boolean
		{
			return bDrag;
		}
		
		public function set isDrag(value:Boolean):void
		{
			//没有添加到舞台不处理
			if(!(this.stage))
				return;
			
			bDrag = value;
			
			UpDataEvent();
		}
		
		public function UpDataEvent():void
		{
			if(bDrag)
			{
				if(!this.hasEventListener(MouseEvent.MOUSE_MOVE))
					this.addEventListener(MouseEvent.MOUSE_MOVE, OnMouseMove);

				this.stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
			}
			else
			{
				if(this.hasEventListener(MouseEvent.MOUSE_MOVE))
					this.removeEventListener(MouseEvent.MOUSE_MOVE, OnMouseMove);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
			}
		}
		
		protected function OnMouseUp(event:MouseEvent):void
		{
				this.stopDrag();
				bDown = false;
		}
		
		protected function OnMouseMove(event:MouseEvent):void
		{
			if(bDown)
			{
				this.startDrag();
			}
		}		
		
	}
}